package com.briup.web;

import com.briup.pojo.Student;
import com.briup.pojo.StudentWithAddress;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/*
 * @Description: 
 * @Author: liuzl
 * @Date: 2024-12-03 09:13:11
 * @LastEditors: liuzl
 * @LastEditTime: 2024-12-03 09:15:53
 */
@RequestMapping("/path")//类级别的RequestMapping定义了这个程序类中所有方法的公共请求路径前缀
@RestController//Controller类必须要添加的 具体后面分解
public class PathController {
    @RequestMapping("test")//处理请求路径和请求方法
    public String test(){
        System.out.println("in test()");
        return "hello,PathController,test()";
    }
    //@RequestMapping(path = {"/hello", "/hi"})//通过path属性指定多个请求路径
    @RequestMapping(value = {"/hello", "/hi"})//通过value属性指定多个请求路径
    public String hello(){
        System.out.println("in hello()");
        return "hello,PathController,hello()";
    }

    //指定请求方法
    //@RequestMapping(value = "/method", method = RequestMethod.POST)//通过value属性指定请求路径 method属性指定请求方法
    @RequestMapping(value = "/method", method = {RequestMethod.POST,RequestMethod.GET})//通过value属性指定请求路径 method属性指定多个请求方法
    public String method(){
        System.out.println("in method()");
        return "hello,PathController,hello()";
    }
    //获取请求参数
    @RequestMapping(value = "/getParams", method={RequestMethod.POST,RequestMethod.GET})//通过params属性指定请求参数
    public String getParams(HttpServletRequest request){
        //获取请求参数
        String name = request.getParameter("name");
        String ageStr = request.getParameter("age");
        int age = Integer.parseInt(ageStr);
        System.out.println("name:"+name);
        System.out.println("age:"+age);
        System.out.println("in getParams()");
        return "ok";
    }
    //参数获取
    @RequestMapping("/simpleParam")
    public String simpleParam(String name,Integer age){
        System.out.println("name:"+name);
        System.out.println("age:"+age);
        return "ok";
    }

    //参数获取-使用@RequestParam解决请求参数名称不一致无法映射成功的问题
    @RequestMapping("/simpleParam2")
    public String simpleParam2(@RequestParam("username") String name, Integer age){
        System.out.println("name:"+name);
        System.out.println("age:"+age);
        return "ok";
    }

    //实体参数-请求参数的名称要和Pojo类的属性名一致，否则无法封装
    @RequestMapping("/simplePojo")
    public String simplePojo(Student stu){
        System.out.println(stu);
        return "ok";
    }

    //嵌套实体参数-请求参数的名称要和Pojo类的属性名一致，否则无法封装
    @RequestMapping("/complexPojo")
    public String complexPojo(StudentWithAddress stu){
        System.out.println(stu);
        return "ok";
    }

    //数组参数-请求参数的名称要和形参中的数组名一致，否则无法封装
    //127.0.0.1:8081/path/arrayParam
    //?likes=book&likes=sing&likes=dance
    @RequestMapping("/arrayParam")
    public String arrayParam(String[] likes){
        System.out.println(Arrays.toString(likes));
        return "ok";
    }

    //集合参数-请求参数的名称要和形参中的数组名一致，否则无法封装
    //127.0.0.1:8081/path/listParam
    //?likes=book&likes=sing&likes=dance
    @RequestMapping("/listParam")
    public String listParam(@RequestParam List<String> likes){
        System.out.println(likes);
        return "ok";
    }

    //集合参数-请求参数的名称要和形参中的数组名一致，否则无法封装
    //127.0.0.1:8081/path/jsonParam
    //必须通过Post请求方式发送JSON数据
    @RequestMapping(value = "/jsonParam",method = {RequestMethod.POST})
    public String jsonParam(@RequestBody StudentWithAddress stu){
        System.out.println(stu);
        return "ok";
    }

    //根据id删除用户
    //127.0.0.1:8081/path/deleteById?id=1
    //127.0.0.1:8081/path/deleteById/1
    //路径参数
    //127.0.0.1:8081/path/jsonParam
    //必须通过Post请求方式发送JSON数据
    @RequestMapping("/deleteById/{id}")
    public String jsonParam(@PathVariable Integer id){
        System.out.println(id);
        return "ok";
    }

    @RequestMapping("/query/{id}/{username}")
    public String query(@PathVariable Integer id,@PathVariable("username") String name){
        System.out.println(id);
        System.out.println(name);
        return "ok";
    }

    //127.0.0.1:8081/path/queryStuById/1
    @RequestMapping("/queryStuById/{id}")
    @ResponseBody//如果要返回对象的json格式数据，需要借助@ResponseBody实现
    public Student queryStuById(@PathVariable Integer id){
        System.out.println(id);
        //返回对象 返回json
        Student student = new Student();
        student.setName("zhangsan");
        student.setAge(23);
        return student;
    }

    @RequestMapping("/queryStu")
    @ResponseBody//如果要返回对象的json格式数据，需要借助@ResponseBody实现
    public List<Student> queryStu(){
        //返回对象 返回json
        Student student = new Student();
        student.setName("zhangsan");
        student.setAge(23);
        Student student1 = new Student();
        student1.setName("lisi");
        student1.setAge(24);
        ArrayList<Student> stuList = new ArrayList<>();
        stuList.add(student);
        stuList.add(student1);
        return stuList;
    }
}